Initial set up

In the app.py file, you need to import necessary modules from Flask and instantiate the Flask application.
For this lab, you will need to import the following functions from the flask library.

  • Flask - to instantiate the application
  • request - to process the GET and POST requests
  • url_for - to access the url for a given function using its decorator
  • redirect - to redirect access requests according to requirement
  • render_template - to render the html page

After importing the functions, instantiate the application to a variable app.

Click here for hint
  1. 1
  1. from flask import <functions>
Click here for solution
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  1. # Import libraries
  2. from flask import Flask, redirect, request, render_template, url_for
  3. # Instantiate Flask functionality
  4. app = Flask(__name__)

Now, the code will look like this:
Correct code reference

Next, let's create a list of sample transactions for testing purposes. You can assume that the transactions already exist on the interface when it is executed for the first time. Please note that this step is completely optional and does not affect the functionality you will develop in this lab. Add the code snippet as shown below to app.py.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  1. # Sample data
  2. transactions = [
  3. {'id': 1, 'date': '2023-06-01', 'amount': 100},
  4. {'id': 2, 'date': '2023-06-02', 'amount': -200},
  5. {'id': 3, 'date': '2023-06-03', 'amount': 300}
  6. ]
Click here for solution

Sample data

The order in which you will develop the functions is as follows:
1. Read
2. Create
3. Update
4. Delete
The reason to implement Read before the other functions is to be able to redirect to the page with all transactions every time a new transaction is created, updated, or deleted. Therefore, the function to read the existing transactions must exist before the others are implemented.